<template>
	<view class="main">
		<view class="head-wrap">
			<view class="head-title-wrap">
				<view>
					<text style="margin-right: 20rpx;">上报科室: {{dept}}</text>
					<text>|</text>
					<text style="margin-left: 20rpx;">提交人: {{creator}}</text>
				</view>
				<view>
					<text style="margin-right: 20rpx;">薪资期间: {{meritsDate}}</text>
				</view>
				<view>
					<text style="margin-right: 20rpx;">提交时间:{{applyDate}}</text>
				</view>
			</view>
			<view class="prograss" @click="checkPrograss">审核进度</view>
		</view>
		<scroll-view class="scroll-x" scroll-x="true" scroll-with-animation="true" enable-flex="true">
			<view class="show-detail">
				<view class="show-detail-sub" v-for="(item,index) in meritsClass" v-show="item.totals!=0">
					<view class="show-detail-box">{{item.totals.toFixed(2)}}</view>
					<view class="show-detail-title">{{item.label}}</view>
				</view>
			</view>
		</scroll-view>
		<view v-for="(item,index) in inform" class="main-sub">
			<person-infrom :propName="item.psnname" :propNum="item.psncode" :propDepartment="item.deptname">
			</person-infrom>
			<scroll-view class="content" scroll-x="true" scroll-with-animation="true">
				<view style="display: flex;">
					<view class="content-sub" v-for="(key,value) in item"
						v-show="value!='psnname'&&value!='psncode'&&value!='deptname'&&value!='sum'&&key!=0">
						<view class="content-sub-lable">{{key}}</view>
						<view class="content-sub-value">{{value}}</view>
					</view>
				</view>
			</scroll-view>
			<view class="person-totals">个人合计:{{item.sum.toFixed(2)}}</view>
		</view>
		<view class="all-totals">
			<view class="all-totals-des">
				<text>总计:</text>
				<text style="margin-left: 10rpx;">{{allMoney.toFixed(2)}}</text>
			</view>
			<view class="btn-wrap" v-if="$store.state.isShowExa==1&&isShowBtn!=1">
				<view class="cancel-exa" @click="cancelExamine" >取消审批</view>
			</view>
			<view class="btn-wrap" v-if="$store.state.isShowExa==1&&isShowBtn==1">
				<view class="adot-btn" @click="exit">驳回</view>
				<view class="confirm-btn" @click="adopt">通过</view>
			</view>
			<assign-module billtype="WAEP" 
							@finishExamine="finishService" 
							:formPk="pk" 
							ref="assignCharacter" >
			</assign-module>
		</view>
	</view>
</template>

<script>
	import service from '../../../common/service/new_file.js';
	import apiData from '../../../common/utils/apiData.js';
	import utils from '../../../common/utils/utils.js';
	export default {
		data() {
			return {
				inform: [],
				pk: '',
				dept: '', //上报科室
				meritsDate: '', //薪资期间\
				applyDate: '',
				meritsClass: [], //绩效的分类
				allMoney: 0, //当前表单总计
				creator: ',',
				isShowBtn:'',
				isExa:0
			};
		},
		methods: {
			adopt(){
				//通过
				this.$refs.assignCharacter.init('1')
			},
			goback(){
				//返回页面
				uni.navigateTo({
					url:"../specialExamine/specialExamine"
				})
			},
			exit(obj) {
				this.$refs.assignCharacter.init('2')
			},
			cancelExamine(){
				this.exitExa(this.pk)
			},
			exitExa(pk) {
				let _this = this;
				let data = apiData.setExitExamineData(pk, 'WAEP'); //FN(billtype, isAdopt,pk)
				service.common(data, {
					success: function(res) {
						uni.showToast({
							icon:'none',
							title: '已撤回,即将退出该页面'
						});
						setTimeout(() => {
							uni.navigateTo({
								url:"../specialExamine/specialExamine"
							})
						}, 1500);
					},
					fail: function(err) {},
					all: function(all) {}
				});
			},
			finishService(){
				this.goback()
			},
			checkPrograss() {
				uni.navigateTo({
					url: "../../attendance/attendacePrograss?pk=" + this.pk + '&billtype=WAEP'
				});
			},
			unique(arr) {
				//去重
				let newArr = []
				arr.forEach(item => {
					if (newArr.filter(el => item.label == el.label).length == 0) {
						newArr.push(item)
					}
				})
				return newArr
			},
			getAllproject(arr) {
				//获取所有项目的费用
				let project = []
				arr.forEach((item, index) => {
					for (let key in item) {
						if (key != 'psnname' && key != 'psncode' && key != 'deptname') {
							//排除掉非项目的属性
							this.allMoney += Number(item[key])
							project.push({
								label: key,
								money: item[key]
							})
						}
					}
				})
				//去重
				let hasUniqueArr = this.unique(project)
				//获取每一项的分数
				hasUniqueArr.forEach(item => {
					let totals = 0
					let filterArr = project.filter((el) => el.label == item.label)
					//获取每一项的综合绩效
					filterArr.forEach(num => {
						totals += Number(num.money)
					})
					item.totals = totals
				})
				this.meritsClass = hasUniqueArr
			},
			getMoneyInperSon(arr) {
				//获取个人的总计费用
				arr.forEach((item, index) => {
					let sum = 0
					for (let key in item) {
						if (key != 'psnname' && key != 'psncode' && key != 'deptname') {
							//排除掉非项目的属性
							sum += Number(item[key])
						}
					}
					item.sum = sum
				})
				console.log(arr)
				return arr
			}
		},
		onLoad(obj) {
			let myObj = JSON.parse(obj.item)
			this.isShowBtn = obj.isShowBtn
			this.getAllproject(myObj.data.bvos)
			this.inform = this.getMoneyInperSon(myObj.data.bvos)
			this.dept = myObj.data.hvo.def1.name
			this.creator = myObj.data.hvo.creator.name
			this.meritsDate = myObj.data.hvo.pk_period
			this.applyDate = myObj.data.hvo.ts
			this.pk = obj.pk
		}
	}
</script>

<style lang="scss" scoped>
	.cancel-exa{
		display: flex;
		align-items: center;
		justify-content: center;
		flex: 1;
		background: #d52b2e;
	}
	.all-totals {
		width: 100%;
		
		position: fixed;
		
		display: flex;
		flex-direction: column;
		font-weight: 400;
		font-size: 34rpx;
		color: #FFFFFF;
		bottom: 0;
		.all-totals-des{
			display: flex;
			align-items: center;
			width: 100%;
			height: 80rpx;
			padding: 0 24rpx;
			background: #0dd65e;
		}
		.btn-wrap{
			width: 100%;
			display: flex;
			height: 80rpx;
			.adot-btn{
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
				background: #818181;
			}
			.confirm-btn{
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
				background: #2282ff;
			}
		}
	}

	.show-detail {
		padding: 20rpx 0;
		color: #fff;
		display: flex;

		.show-detail-sub {
			width: 160rpx;
			margin: 0 20rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.show-detail-box {
				font-size: 34rpx;
				font-weight: 600;
			}

			.show-detail-title {
				font-size: 26rpx;
			}
		}
	}

	.person-totals {
		font-size: 26rpx;
		padding: 10rpx 0;
		color: #4a454b;
	}

	.scroll-x {
		background: #7bb0ff;
		width: 100%;
		white-space: nowrap;
		margin-bottom: 20rpx;
	}

	.head-title-wrap {
		color: #585858;
		font-size: 26rpx;
		font-weight: 600;
	}

	.head-wrap {
		padding: 20rpx 24rpx;
		color: #0051FF;
		background: #fff;
		display: flex;
		justify-content: space-between;
	}

	.main {
		padding-bottom: 180rpx;
		.main-sub {
			padding: 0 24rpx;
			margin-bottom: 30rpx;
			border-bottom: 2rpx solid #ebebeb;
		}
	}

	.content {
		width: 90%;
		display: flex;
		white-space: nowrap;

		.content-sub {
			margin: 25rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.content-sub-lable {
				color: #0051FF;
				font-weight: 500;
				font-size: 32rpx;
				margin-top: 6rpx;
			}

			.content-sub-value {
				font-weight: 500;
				font-size: 26rpx;
				margin-top: 6rpx;
			}
		}
	}
</style>
